package rcs34.android.photopublishare;

import java.util.Set;
import java.util.Map.Entry;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnLongClickListener;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.Toast;

public class ActivitySelectTarget extends ListActivity {
	private final String LOGTAG = getClass().getName();
	private final int REQUEST_SET_TARGET = 1;
	public final static String EXTRA_SELECTED_TARGETS="extra_selected_targets";
	private OnLongClickListener longClickListener;
	private int contextMenuPosition=0;
	private ProgressDialog progressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_target);
		registerForContextMenu(getListView());
		longClickListener = new View.OnLongClickListener() {
			public boolean onLongClick(View v) {
				Log.v(LOGTAG,v.getId()+"");
				openContextMenu(v);
				return true;
			}
		};
		setListAdapter(new AdapterSelectTarget(this));
	}
    public OnLongClickListener getLongClickListener(){
    	return longClickListener;
    }
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
		 contextMenuPosition = info.position;
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.target_context_menu, menu);
	}

	public boolean onContextItemSelected(MenuItem _item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) _item
				.getMenuInfo();
		ContentValues item = GApp.getConfiguredTargets().get(contextMenuPosition);
		switch (_item.getItemId()) {
		case R.id.edit:
			Intent intent = new Intent(this, ActivitySetTarget.class);
			intent.putExtra(DBHelper.COL_NAME, item.getAsString(DBHelper.COL_NAME));
			intent.putExtra(DBHelper.COL_URL, item.getAsString(DBHelper.COL_URL));
			intent.putExtra(DBHelper.COL_LOGIN, item.getAsString(DBHelper.COL_LOGIN));
			intent.putExtra(DBHelper.COL_PASSWORD, item.getAsString(DBHelper.COL_PASSWORD));
			intent.putExtra(DBHelper.COL_PORT, item.getAsString(DBHelper.COL_PORT));
			intent.putExtra(DBHelper.COL_ROWID, item.getAsString(DBHelper.COL_ROWID));
			startActivityForResult(intent, REQUEST_SET_TARGET);
			return true;
		case R.id.delete:
			Deleter deleter=new Deleter();
			deleter.execute(item.getAsString(DBHelper.COL_ROWID));
			return true;
		default:
			return super.onContextItemSelected(_item);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_SET_TARGET) {
			((AdapterSelectTarget) getListAdapter()).dataChanged();
		}
	}

	public void handleNew(View _view) {
		Intent intent = new Intent(this, ActivitySetTarget.class);
		startActivityForResult(intent, REQUEST_SET_TARGET);
	}

	public void handleNext(View _view) {
		Intent intent = new Intent(this, ServiceUpload.class);
		String selected="";
		for(ContentValues target:GApp.getConfiguredTargets()){
			if(target.getAsString(DBHelper.COL_SELECTED).equals("1")){
				selected+=target.getAsString(DBHelper.COL_ROWID)+";";
			}
		}
		if(selected.length()<1){
			Toast.makeText(GApp.context, R.string.err_no_targets_selected, Toast.LENGTH_SHORT).show();
			return;
		}
		intent.putExtra(EXTRA_SELECTED_TARGETS, selected);
		Set<Entry<String,Object>> entries=GApp.imageExif.valueSet();
		for(Entry<String,Object> entry : entries){
			intent.putExtra(entry.getKey(),(String)entry.getValue());
		}
		progressDialog=ProgressDialog.show(this, "Saving", "");
		Saver saver=new Saver();
		saver.execute(0);
		startService(intent);
	}

	public void handleBack(View _view) {
		Intent intent = new Intent(this, ActivityOptions.class);
		startActivity(intent); 
	}
	
	private class Saver extends AsyncTask<Integer, Integer, Integer> {
		@Override
		protected Integer doInBackground(Integer ... _dummy) {
			DBHelper dbHelper=new DBHelper();
			dbHelper.updateAll(GApp.getConfiguredTargets());
			return 0;
		}
		@Override
		protected void onPostExecute(Integer _dummy) {
			progressDialog.dismiss();
		}
	}
	
	private class Deleter extends AsyncTask<String, Integer, Integer> {
		@Override
		protected Integer doInBackground(String... _rowid) {
			DBHelper dbHelper=new DBHelper();
			dbHelper.deleteRecord(_rowid[0]);
			return 0;
		}
		@Override
		protected void onPostExecute(Integer _dummy) {
			((AdapterSelectTarget) getListAdapter()).dataChanged();
		}
	}
}
